<template>
    <div class="score-container">
        <el-radio-group v-model="scoreType" @change="changeScoreType">
            <el-radio label="1">患者基本信息</el-radio>
            <el-radio label="2">住院过程信息</el-radio>
            <el-radio label="3">诊疗信息</el-radio>
            <el-radio label="4">费用信息</el-radio>
        </el-radio-group>
        <el-tabs v-model="activeNameTable">
            <el-tab-pane name="index" label="首页">
                <el-table :data="scoreData"
                          ref="detailstable"
                          class="score-table"
                          border
                          :max-height="maxHeight">
                    <el-table-column label="项目" align="center" prop="messageType" min-width="70">
                        <template slot-scope="scope">
                            {{getScoreType(scope.row.messageType)}}
                        </template>
                    </el-table-column>
                    <el-table-column
                            label="类别"
                            align="center"
                            prop="messageClass"
                            min-width="40">
                        <template slot-scope="scope">
                            {{getScoreClass(scope.row.messageClass)}}
                        </template>
                    </el-table-column>
                    <el-table-column
                            label="评分项"
                            prop="name"
                            min-width="170">
                        <template slot-scope="scope">
                            <span :class="scope.row.scoreType ? 'rating-items yellow' : 'rating-items blue'"></span>
                            {{scope.row.name}}
                        </template>
                    </el-table-column>
                    <el-table-column
                            label="填写值"
                            align="center"
                            prop="content"
                            min-width="100">
                    </el-table-column>
                    <el-table-column
                            label="分值"
                            align="center"
                            prop="scoreValue"
                            min-width="40">
                    </el-table-column>
                    <el-table-column
                            label="缺陷"
                            align="center"
                            prop="缺陷"
                            min-width="40">
                        <template slot-scope="scope">
                            <el-checkbox v-model="scope.row.isDeduction" :disabled="scope.row.isAuto"
                                         @change="changeScore(scope.row)">
                            </el-checkbox>
                        </template>
                    </el-table-column>
                    <el-table-column
                            label="扣分"
                            align="center"
                            prop="deduction"
                            min-width="35">
                        <template slot-scope="scope">
        <span v-if="scope.row.deduction > 0" style="color:red">
        {{-scope.row.deduction}}
        </span>
                            <span v-else>{{scope.row.deduction}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="原因" prop="reason" min-width="200">
                        <template slot-scope="scope">
                            <el-select v-if="scope.row.isDeduction && !scope.row.isAuto" size="small"
                                       v-model="scope.row.reason" placeholder="请选择扣分原因"
                                       @change="changeScore(scope.row, 'select')">
                                <el-option :label="scope.row.name+'填写不规范'" :value="scope.row.name+'填写不规范'"
                                           :key="0"></el-option>
                                <el-option :label="scope.row.name+'填写遗漏'" :value="scope.row.name+'填写遗漏'"
                                           :key="1"></el-option>
                                <el-option :label="scope.row.name+'填写错误'" :value="scope.row.name+'填写错误'"
                                           :key="2"></el-option>
                            </el-select>
                            <span v-else-if="scope.row.deduction > 0 && scope.row.isAuto">{{scope.row.reason}}</span>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tab-pane>
            <el-tab-pane name="appendix" label="附页" disabled></el-tab-pane>
        </el-tabs>
        <div class="dialog-footer">
            <el-row class="footer-score-box">
                <el-col :span="8" class="national">
                    <span>{{nationalScore}}</span><span>分</span>
                    <p>国标得分</p>
                </el-col>
                <el-col :span="8" class="hospital">
                    <span>{{-hospitalScore}}</span><span>分</span>
                    <p>院标扣分</p>
                </el-col>
                <el-col :span="8" class="deduction">
                    <span>{{-hospitalDeduction}}</span><span>分</span>
                    <p>扣分</p>
                </el-col>
            </el-row>
            <div class="btn-box">
                <el-button size="small" @click="closeDialogScore">取 消</el-button>
                <el-button size="small" @click="submitScore" type="primary">确 定</el-button>
            </div>
        </div>
    </div>
</template>

<script>
    import {getScoreNum, submitScoreNum, getScoreList} from '@/api/info-tool'

    export default {
        name: 'Score',
        data () {
            return {
                // 显示小助手
                showLittleHelper: true,
                // 小助手当前标签
                activeHelper: 'coding',
                // 显示意见反馈弹框
                isShowFeedback: false,
                // 显示临床反馈弹框
                isShowMRFeedback: false,
                // 类型选项
                typeOptions: [
                    {'key': 1, 'value': '编码问题'},
                    {'key': 2, 'value': '非编码问题'},
                    {'key': 3, 'value': '评分问题'},
                    {'key': 4, 'value': '统计问题'},
                    {'key': 5, 'value': '其他问题'}
                ],
                // 意见反馈表单
                feedbackForm: {
                    feedbackKind: '',
                    content: ''
                },
                // 临床反馈表单
                MRFeedbackForm: {
                    content: ''
                },
                // buffer列表
                bufferArr: [],
                // 图片列表
                imgList: [],
                // 预览图片路径
                dialogImageUrl: '',
                // 显示预览图片
                dialogImgVisible: false,
                // 评分弹框显示标签页
                activeNameTable: 'index',
                // 显示评分弹框
                isShowScore: false,
                // 评分类型
                scoreType: '1',
                // 评分列表数据
                scoreData: [],
                // 所有的扣分，项包括自动扣分项
                detailIdItem: [],
                // 国标得分
                nationalScore: 0,
                // 院标得分
                hospitalScore: 0,
                // 医院扣分
                hospitalDeduction: 0,
                // 评分获取的所有数据
                allScoreData: [],
                // 显示ICD查询
                showICD: false,
                // ICD查询内容
                searchICD: {
                    typeYB: '国临版2.0',
                    type: 'ICD-10',
                    word: '',
                    category: '',
                    selectivity: ''
                },
                // 显示ICD查询结果/查询页面
                showICDSearchRes: false,
                // ICD查询数据
                ICDRes: [],
                // ICD数据总数
                ICDTotal: 0,
                // ICD单页数量
                ICDPageSize: 50,
                // ICD当前页
                currentPageICD: 1,
                // 默认展开DRG
                activeCollapse: '1',
                userType: '',
                // 显示drg信息
                showDRGInfo: false,
                // 计时器
                timer: null,
                rules: {
                    feedbackKind: [
                        {required: true, message: '请选择反馈类型', trigger: 'change'}
                    ],
                    content: [
                        {required: true, message: '请输入反馈内容', trigger: ['blur', 'change']}
                    ]
                },
                // 搜索病案
                keyword: '',
                tabsType: '质控信息',
                infoData: {
                    noneCodingError: [],
                    noneCodingWaring: [],
                    codingError: [],
                    codingWaring: [],
                    allDeduction: [],
                    DRGsData: {},
                    resExamine: []
                },
                hospitalTime: '',
                medicalRecordSign: '',
                medicalRecordID: '',
                maxHeight: '550px'
            }
        },
        created () {
            this.timer = setInterval(this.getMaxHeight, 10)
            let data = localStorage.getItem('scoreData')
            this.hospitalTime = JSON.parse(data).hospitalTime
            this.medicalRecordSign = JSON.parse(data).medicalRecordSign
            this.medicalRecordID = JSON.parse(data).medicalRecordID
            this.getScoreTableData()
        },
        mounted () {
        },
        methods: {
            /**
             * 获取最大高度
             */
            getMaxHeight () {
                this.maxHeight = document.getElementsByClassName('score-container')[0].offsetHeight - 157 + 'px'
            },
            /**
             * 打开得分列表获取数据
             */
            getScoreTableData (type) {
                let params = {
                    medicalRecordSign: this.medicalRecordSign,
                    hospitalTime: this.hospitalTime,
                    isLast: true,
                    orgId: this.getLocalStorage('userMechanism', 'id')
                }
                this.isShowScore = true
                this.hospitalScore = 0
                getScoreList(params).then(res => {
                    if (res.code.toString() === '200') {
                        this.scoreType = '1'
                        this.allScoreData = res.data
                        this.detailIdItem = []
                        this.allScoreData.forEach(item => {
                            if (item.deduction > 0 && item.isDeduction) {
                                this.detailIdItem.push({
                                    field: item.field,
                                    reason: item.reason,
                                    scoreType: item.scoreType,
                                    fieldName: item.name,
                                    nationDeduction: item.deduction,
                                    msg: item.reason.split('|')
                                })
                            }
                        })
                        this.getScore(type)
                        this.changeScoreType()
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            /**
             * 修改评分类型
             */
            changeScoreType () {
                this.scoreData = []
                switch (this.scoreType) {
                case '1':
                    this.allScoreData.forEach(item => {
                        if (item.messageType === 1) { this.scoreData.push(item) }
                    })
                    break
                case '2':
                    this.allScoreData.forEach(item => {
                        if (item.messageType === 2) { this.scoreData.push(item) }
                    })
                    break
                case '3':
                    this.allScoreData.forEach(item => {
                        if (item.messageType === 3) { this.scoreData.push(item) }
                    })
                    break
                case '4':
                    this.allScoreData.forEach(item => {
                        if (item.messageType === 4) { this.scoreData.push(item) }
                    })
                    break
                }
            },
            /**
             * 修改评分
             * @param row 行数据
             * @param type 修改方式select是0分
             */
            changeScore (row, type) {
                let index = 0
                if (!row.isDeduction && this.detailIdItem.length > 0) {
                    for (let i = 0; i < this.detailIdItem.length; i++) {
                        if (this.detailIdItem[i].field === row.field && this.detailIdItem[i].scoreType === row.scoreType) {
                            index = i
                            break
                        }
                    }
                    if (index >= 0) {
                        this.detailIdItem.splice(index, 1)
                        this.getScore()
                    }
                    row.deduction = 0
                } else {
                    if (this.detailIdItem.length >= 0) {
                        for (let i = 0; i < this.detailIdItem.length; i++) {
                            if (this.detailIdItem[i].field === row.field && this.detailIdItem[i].scoreType === row.scoreType) {
                                index = i
                                break
                            }
                        }
                        if (this.detailIdItem.length > 0 &&
                            this.detailIdItem[index].field === row.field && this.detailIdItem[index].scoreType === row.scoreType) {
                            this.detailIdItem[index].reason = row.reason
                            return
                        } else {
                            this.detailIdItem.push({
                                field: row.field,
                                reason: row.reason,
                                scoreType: row.scoreType
                            })
                        }
                        this.getScore()
                        row.deduction = type ? 0 : row.scoreValue
                    }
                }
            },
            /**
             * 计算分数
             */
            getScore (type) {
                let params = {
                    detailId: this.medicalRecordID,
                    items: this.detailIdItem,
                    orgId: this.getLocalStorage('userMechanism', 'id')
                }
                getScoreNum(params).then(res => {
                    if (res.code.toString() === '200') {
                        this.hospitalScore = res.data.yyTotalScore
                        this.nationalScore = res.data.gbTotalScore
                        this.hospitalDeduction = res.data.deduction
                        if (type === 'submit') {
                            /* eslint-disable */
                            native.invoke('AssistantWindow', 'returnSearch', JSON.stringify({data: this.detailIdItem, score: res.data}))
                            /* eslint-enable */
                        }
                    } else {
                        this.$message.error(res.message)
                    }
                })
            },
            /**
             * 提交修改评分
             */
            submitScore () {
                let flag = true
                for (let i = 0; i < this.detailIdItem.length; i++) {
                    if (!this.detailIdItem[i].reason) {
                        this.$message.error('扣分原因不能为空!')
                        flag = false
                        break
                    }
                }
                if (flag) {
                    let params = {
                        detailId: this.medicalRecordID,
                        items: this.detailIdItem,
                        orgId: this.getLocalStorage('userMechanism', 'id'),
                        dischargeDate: this.parseTime(new Date().getTime())
                    }
                    submitScoreNum(params).then(res => {
                        if (res.code.toString() === '200') {
                            this.$message.success('提交评分成功!')
                            this.getScoreTableData('submit')
                        } else {
                            this.$message.error(res.message)
                        }
                    }).catch(() => {
                        this.$message.error('提交失败!')
                    })
                }
            },
            /**
             * 获取信息类型
             * @param val 类型值
             */
            getScoreType (val) {
                switch (val.toString()) {
                case '1':
                    return '患者基本信息'
                case '2':
                    return '住院过程信息'
                case '3':
                    return '诊疗信息'
                case '4':
                    return '费用信息'
                }
            },
            /**
             * 获取信息类别
             * @param val 类别值
             */
            getScoreClass (val) {
                switch (val.toString()) {
                case '1':
                    return 'A类'
                case '2':
                    return 'B类'
                case '3':
                    return 'C类'
                case '4':
                    return 'D类'
                }
            },
            /**
             * 关闭评分弹框
             */
            closeDialogScore () {
                /* eslint-disable */
                native.close()
                /* eslint-enable */
            }
        },
        beforeDestroy () {
            clearInterval(this.timer)
        }
    }
</script>

<style scoped lang="scss">
    .score-container {
        height: 100vh;
        box-sizing: border-box;
        padding: 10px 20px;
        .el-radio-group {
            width: 100%;
            text-align: right;
        }
        .score-table {
            .rating-items {
                width: 6px;
                height: 12px;
                display: inline-block;
                vertical-align: middle;
                margin: 0;
                border-radius: 2px;
                &.blue {
                    background: #1297dc;
                }
                &.yellow {
                    background: #f29716;
                }
            }
        }
        .dialog-footer {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .footer-score-box {
                width: 50%;
                p {
                    margin: 0;
                    font-weight: 500;
                }
                span:first-child {
                    font-size: 24px;
                }
                .national {
                    color: #66a0f2;
                }
                .hospital {
                    color: #ff8100;
                }
                .deduction {
                    color: #e21f06;
                }
            }
        }
    }
</style>
